﻿namespace Graph;

public class DFSGraph
{
    private IGraph G;
    private bool[] visited;

    public MyList<int> ResultList { get; } = new MyList<int>();

    public DFSGraph(IGraph G)
    {
        this.G = G;
        visited = new bool[G.V];

        //DFS(0);
        for (int v = 0; v < G.V; v++)
        {
            if (!visited[v])
            {
                DFS(v);
            }
        }
    }

    private void DFS(int v)
    {
        visited[v] = true;
        ResultList.Add(v);

        foreach (int w in G.Adj(v))
        {
            if (!visited[w])
            {
                DFS(w);
            }
        }
    }

    //验证
    static void Main(string[] args)
    {
        IGraph graph = new AdjLinkList("图的深度优先遍历/g2.txt");
        DFSGraph fSGraph = new(graph);
        Console.WriteLine(fSGraph.ResultList);

        Console.Read();
    }
}
